package com.ving.test.mybatis02.session;

import com.ving.test.mybatis02.binding.MapperMethod02;
import com.ving.test.mybatis02.binding.MapperRegister02;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuweijie on 2019-11-19.
 */
public class Configuration02 {
    private InputStream inputStream;
    private MapperRegister02 mapperRegister02 = new MapperRegister02();
    ;

    public Configuration02() {
    }

    public Configuration02(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public MapperRegister02 getMapperRegister02() {
        return mapperRegister02;
    }

    public void setMapperRegister02(MapperRegister02 mapperRegister02) {
        this.mapperRegister02 = mapperRegister02;
    }

    /**
     * 开始读取配置文件，加载配置内容
     */
    public void loadConfigrution(){
        // 读取流
        try {
            Document doc = new SAXReader().read(inputStream);
            Element root = doc.getRootElement();
            List<Element> elementList = root.element("mappers").elements("mapper");
            for (Element element : elementList) {

                if (element.attribute("resource")!=null) {
                    mapperRegister02.setMapperRegister(loadxmlConfiguration(element.attribute("resource").getText()));

                }else if(element.attribute("class")!=null){
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据mybatis-config.xml指定的自定义xml文件路径，去读取xml文件内容
     * @param resource
     * @return
     */
    private Map<String, MapperMethod02> loadxmlConfiguration(String resource) {
        Map<String, MapperMethod02> map = new HashMap<>();
        // 用类类型获取读取资源器，用相对路径读取配置文件
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(resource);
        try {
            // 解析xml格式的工具类
            Document read = new SAXReader().read(resourceAsStream);
            // 获取根节点
            Element rootElement = read.getRootElement();

            if(rootElement.getName().equalsIgnoreCase("mapper")){
                String nameSpace = rootElement.attribute("namespace").getText();
                List<Element> elementList = rootElement.elements("select");
                for (Element element : elementList) {
                    MapperMethod02 mapperMethod01 = new MapperMethod02<>();
                    mapperMethod01.setSql(element.getText().trim());
                    mapperMethod01.setType(Class.forName(element.attribute("resultType").getText()));
                    // namespace + "."+id
                    String key = String.format("%s.%s", nameSpace, element.attribute("id").getText());
                    map.put(key,mapperMethod01);
                }
            }
        } catch (DocumentException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                resourceAsStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return map;
    }
}
